package code.cando;

/**
 * author : yeswater
 * date : 2023/12/7
 */

public class T034 {
    public static void main(String[] args) {
        int[] nums = {1,2,3};
        int target = 1;
        int[] res = searchRange(nums, target);
        System.out.println(res[0] + " " + res[1]);
    }
    public static int[] searchRange(int[] nums, int target) {
        int L = 0;
        int R = nums.length - 1;
        int mid = 0;
        int minIndex = -1;
        int maxIndex = -1;
        while (L <= R) {  // 这里记得加 =
            mid = (L + R) >> 1;
            if (nums[mid] == target) {
                minIndex = mid;
                maxIndex = mid;
            }
            if (nums[0] <= target && target < nums[mid]) { // 这里记得加 =
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        
        while (minIndex >= 1) {
            if (nums[minIndex] == nums[minIndex - 1]) {
                --minIndex;
            } else {
                break; // 这里记得结束循环
            }
        }
        while (maxIndex < nums.length - 2 && maxIndex >= 0) {
            if (nums[maxIndex] == nums[maxIndex] + 1) {
                ++maxIndex;
            } else {
                break; // 这里记得结束循环
            }
        }
        return new int[]{minIndex, maxIndex};
    }
}
